default_platform(:mac)

# https://docs.fastlane.tools/actions/build_mac_app/#automating-the-whole-process
platform :mac do
  lane :local do
    desc "Build a local release version"

    # https://docs.fastlane.tools/actions/xcode_select/#xcode_select
    xcode_select "/Applications/Xcode.app"

    build_and_zip("Local Release", false, true)
  end

  lane :beta do
    desc "Build a beta release version"

    # https://docs.fastlane.tools/actions/xcode_select/#xcode_select
    # https://github.com/actions/runner-images/tree/main/images/macos
    # https://github.com/actions/runner-images/blob/main/images/macos/macos-15-arm64-Readme.md
    xcode_select "/Applications/Xcode_16.app"

    # http://docs.fastlane.tools/plugins/available-plugins/#semantic_release
    # https://github.com/xotahal/fastlane-plugin-semantic_release
    isReleasable = analyze_commits(match: 'beta*')

    if isReleasable
      build_and_zip("Beta Release", true, false)
    else
      print("Not flagged as a release candidate, skipping the beta release.")
    end
  end

  lane :production do
    desc "Build a production release version"

    # https://docs.fastlane.tools/actions/xcode_select/#xcode_select
    # https://github.com/actions/runner-images/tree/main/images/macos
    # https://github.com/actions/runner-images/blob/main/images/macos/macos-15-arm64-Readme.md
    xcode_select "/Applications/Xcode_16.app"

    # http://docs.fastlane.tools/plugins/available-plugins/#semantic_release
    # https://github.com/xotahal/fastlane-plugin-semantic_release
    isReleasable = analyze_commits(match: 'v*')

    if isReleasable
      build_and_zip("Release", false, false)
    else
      print("Not flagged as a release candidate, skipping the production release.")
    end
  end
end

def build_and_zip(tag_prefix, is_beta, is_local)
  # https://docs.fastlane.tools/plugins/available-plugins/#versioning
  # https://github.com/SiarheiFedartsou/fastlane-plugin-versioning
  unless is_local
    increment_version_number_in_xcodeproj(
      version_number: is_beta ? "v#{lane_context[SharedValues::RELEASE_NEXT_VERSION]}-beta-#{lane_context[SharedValues::RELEASE_LAST_TAG_HASH]}" : lane_context[SharedValues::RELEASE_NEXT_VERSION]
    )
  end

  # https://docs.fastlane.tools/actions/build_mac_app/#parameters
  gym(
    scheme: "auto-clicker",
    workspace: "auto-clicker.xcworkspace",
    clean: true,
    output_directory: "build",
    output_name: "AutoClicker",
    export_method: "mac-application"
  )

  unless is_beta or is_local
    # https://github.com/jonathanneilritchie/fastlane-plugin-find_replace_string
    find_replace_string(
      path_to_file: "README.md",
      old_string: "#{lane_context[SharedValues::RELEASE_LAST_VERSION]}",
      new_string: "#{lane_context[SharedValues::RELEASE_NEXT_VERSION]}",
    )
  end

  unless is_local
    # https://docs.fastlane.tools/actions/commit_version_bump/
    commit_version_bump(
      message: "chore: Fastlane automated version bump#{is_beta ? ' (beta)' : ''}",
      xcodeproj: "auto-clicker.xcodeproj",
      include: %w[Gemfile.lock README.md]
    )

    # https://docs.fastlane.tools/actions/push_to_git_remote/#push_to_git_remote
    push_to_git_remote

    # https://docs.fastlane.tools/actions/add_git_tag/#add_git_tag
    add_git_tag(
      tag: is_beta ? "beta/v#{lane_context[SharedValues::RELEASE_NEXT_VERSION]}/#{lane_context[SharedValues::RELEASE_LAST_TAG_HASH]}" : "v#{lane_context[SharedValues::RELEASE_NEXT_VERSION]}"
    )

    # https://docs.fastlane.tools/actions/push_to_git_remote/#push_to_git_remote
    push_to_git_remote

    # http://docs.fastlane.tools/plugins/available-plugins/#semantic_release
    # https://github.com/xotahal/fastlane-plugin-semantic_release
    notes = conventional_changelog(
      format: 'markdown',
      title: 'macOS Auto Clicker',
      display_title: false,
      commit_url: 'https://github.com/othyn/macos-auto-clicker/commit',
      sections: {
        feat: ":star2: Features",
        fix: ":bug: Bug Fixes",
        refactor: ":recycle: Code Refactoring",
        perf: ":rocket: Performance Improvements",
        chore: ":wrench: Chores",
        test: ":vertical_traffic_light: Testing",
        docs: ":book: Documentation",
        no_type: ":ghost: Unknown... spooky!"
      }
    )
  end

  # https://docs.fastlane.tools/actions/zip
  output_archive_path = zip(
    path: "build/AutoClicker.app",
    output_path: "build/AutoClicker.zip",
    verbose: true
  )

  # https://docs.fastlane.tools/actions/set_github_release/#set_github_release
  unless is_local
    set_github_release(
      repository_name: "othyn/macos-auto-clicker",
      api_bearer: ENV["GITHUB_TOKEN"],
      name: "#{tag_prefix} #{lane_context[SharedValues::RELEASE_NEXT_VERSION]}#{is_beta ? " (#{lane_context[SharedValues::RELEASE_LAST_TAG_HASH]})" : ""}",
      tag_name: "#{is_beta ? "beta/" : ""}v#{lane_context[SharedValues::RELEASE_NEXT_VERSION]}#{is_beta ? "/#{lane_context[SharedValues::RELEASE_LAST_TAG_HASH]}" : ""}",
      description: notes,
      is_prerelease: is_beta,
      commitish: "main",
      upload_assets: [
        output_archive_path,
        "build/AutoClicker.app.dSYM.zip"
      ]
    )
  end
end
